myApp.controller('changeAttrController',function($scope,$state,toaster,$serAttr,$stateParams,FileUploader,$serImgUpload) {
    var ctrl = this;
    $scope.fileItem='';
    ctrl.vm = {
        flag300:[],
        name:'',
        alias:'',
        backend_show:'',
        fliter_show:'',
        type:'',
        addChange:function(){
            if(ctrl.vm.backend_show==true){
                ctrl.vm.backend_show=1
            }else{
                ctrl.vm.backend_show=0
            }
            if(ctrl.vm.fliter_show==true){
                ctrl.vm.fliter_show=1
            }else{
                ctrl.vm.fliter_show=0
            }
            $serAttr.updateAttr({
                id:$stateParams.id,
                name:ctrl.vm.name,
                alias:ctrl.vm.alias,
                fliter_show: ctrl.vm.fliter_show,
                type:ctrl.vm.type,
                backend_show:ctrl.vm.backend_show
            }).then(function(res){
                if(res.code==1){
                    vm.initData();
                    toaster.success('提示',res.message);
                }else{
                    toaster.info('提示',res.message);
                }
            })
        },
        change:function (index) {
            ctrl.vm.flag300.splice(index,1,1);
        },
        data:{},
        adDa:{
            name:'',
            sort:'',
            check:'',
        },
        addAttrS:function(){
            ctrl.vm.adDa.check = ctrl.vm.adDa.check == true ? 1 : 0;
            $serAttr.addAttrValue({
                attr_id:$stateParams.id,
                name:ctrl.vm.adDa.name,
                backend_show:ctrl.vm.adDa.check,
                sort:ctrl.vm.adDa.sort,
                icon:ctrl.vm.adDa.icon
            }).then(function(res){
                if(res.code==1){
                    ctrl.vm.adDa.name='';
                    ctrl.vm.adDa.check=false;
                    ctrl.vm.adDa.sort='';
                    ctrl.vm.adDa.icon='';
                    toaster.success('提示',res.message);
                    vm.init();
                }else{
                    toaster.info('提示',res.message);
                }
            })
        },
        upData:function(data,index){
            $serAttr.updateAttrValue({
                id:data.id,
                name:data.name,
                backend_show: data.backend_show,
                sort:data.sort,
                icon:data.icon
            }).then(function(res){
                if(res.code==1){
                    toaster.success('提示',res.message);
                    vm.init();
                    ctrl.vm.flag300[index]=undefined;
                }else{
                    //ctrl.vm.flag300[index]=undefined;
                    toaster.info('提示',res.message);
                }
            })
        }
    };
    var  vm = {
        init:function(){
            $serAttr.getAttrValueList({
                attr_id:$stateParams.id,
                page:1,
                size:100
            }).then(function(res){
                if(res.code==1){
                    ctrl.vm.data = res.data;
                }else{
                    toaster.info('提示',res.message);
                }
            })
        },
        initData:function(){
            $serAttr.getAttr({
                id:$stateParams.id
            }).then(function(res){
                if(res.code==1){
                    ctrl.vm.name = res.data.base.name;
                    ctrl.vm.alias = res.data.base.alias;
                    ctrl.vm.backend_show = res.data.base.backend_show==1 ?true:false;
                    ctrl.vm.fliter_show = res.data.base.fliter_show==1 ?true:false;
                    ctrl.vm.type = res.data.base.type;
                }else{
                    toaster.info('提示',res.message)
                }
            })
        }
    };

    var uploader = $scope.uploader = $serImgUpload.setImgUpload('a_',true);
    $scope.clearItems = function (index) {
        if(!index){
            $serImgUpload.putImgUpload(uploader,'a',function(){
                $serImgUpload.upload(uploader);
            });
            uploader.onSuccessItem = function(fileItem) {
                ctrl.vm.adDa.icon = fileItem.file.name;
                ctrl.vm.flag300[index]=1;
            };
        }else {
            $serImgUpload.putImgUpload(uploader,'a',function(){
                $serImgUpload.upload(uploader);
            });
            uploader.onSuccessItem = function(fileItem) {
                ctrl.vm.data[index].image = fileItem.file.name;
                ctrl.vm.flag300[index]=1;
            };
        }

    }

    //图片上传end
    vm.init();
    vm.initData();
});